/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_ttc8_tti_rxh.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2014/10/23 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>

#include "frcomm.h"
#include "frstr.h"
#include "frmem.h"
#include "frlog.h"

#include "conntrack.h"
#include "frct_comm.h"

#include "../../ora_core.h"
#include "../../comm/ora_comm.h"
#include "../../comm/ora_util.h"
#include "../ora_mar_engine.h"
#include "ora_ttc8_tti_rxh.h"
#include "ora_ttc8_tti_rxd.h"


/*******************************************************************************
*  FUNC     :  对应于 void unmarshalV10(T4CTTIrxd paramT4CTTIrxd) 方法
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_ttc8_tti_rxh_unmarshalV10(Conntrack *p_st_conn
                                , L7session_ora *p_st_sess
                                , L7_ora_packet *p_st_ora_packet)
{
    short s_rxhflg = 0;
    int i_tmp = 0;
    int i_tmp_len = 0;
    int i_numRqsts = 0;
    int i_iterNum = 0;
    char *p_tmp = NULL;
    int i_byte_num = 0;
    int i_byte_num2 = 0;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_rxhflg) != FR_SUCC) // rxhflg
        goto error;

    if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_numRqsts) != FR_SUCC) // numRqsts
        goto error;

    if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_iterNum) != FR_SUCC) // iterNum
        goto error;

    if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_tmp) != FR_SUCC) // numItersThisTime
        goto error;

    if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_tmp) != FR_SUCC) // uacBufLength
        goto error;

    if (p_st_sess->i_use_special_field == FR_YES)
    {
#if APM_ORA_DBG
        int i_pos_start = bytebuff_getPosition(p_st_ora_packet->p_st_packet);
#endif
        // 0x02 0x05 0x0a 0x1a 0x22
        if (s_rxhflg == 0x02 || s_rxhflg == 0x05 || s_rxhflg == 0x0a || s_rxhflg == 0x1a || s_rxhflg == 0x22)
        {
            if (p_st_sess->i_ttc8_stmt_req_i == FR_YES) // p_st_sess->s_tns_version >= 313
            {
                if (ora_mar_skip(p_st_ora_packet, 4) != FR_SUCC)
                    goto error;
            }

            if (ora_mar_unmarshalDALC(p_st_sess, p_st_ora_packet, &p_tmp, &i_tmp_len) == FR_SUCC) // bitVector
            {
                if (ora_ttc8_tti_rxd_readBitVector(p_st_conn, p_tmp, i_tmp_len) != FR_SUCC)
                {
                    ora_free((void **) &p_tmp);
                    goto error;
                }
                ora_free((void **) &p_tmp);
            }
            else
                goto error;

            if (ora_mar_skipDALC(p_st_sess, p_st_ora_packet) != FR_SUCC)
                goto error;
        }
        else
        {
            if (ora_mar_skip(p_st_ora_packet, 8) != FR_SUCC)
                goto error;
            if (p_st_sess->i_ttc8_stmt_req_i == FR_YES) // p_st_sess->s_tns_version >= 313
            {
                if (ora_mar_skip(p_st_ora_packet, 4) != FR_SUCC)
                    goto error;
            }
            if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_byte_num) != FR_SUCC) // bitVectors的个数
                goto error;
            if (ora_mar_skip(p_st_ora_packet, 2) != FR_SUCC)
                goto error;
            if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_byte_num2) != FR_SUCC) // bitVectors的个数
                goto error;
            if (ora_mar_skip(p_st_ora_packet, 6) != FR_SUCC)
                goto error;
            if (p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_YES) // p_st_sess->c_ttc_version >= 4 && p_st_sess->i_ptr_len == APM_ORA_PTR_LEN_8
            {
                if (ora_mar_skip(p_st_ora_packet, 16) != FR_SUCC)
                    goto error;
                i_byte_num = i_byte_num2;
            }
        }

        if (s_rxhflg == 0x00 && i_byte_num > 0)
        {
            if (ora_ttc8_tti_rxd_unmarshalBVC(p_st_conn, p_st_sess, p_st_ora_packet, i_byte_num) != FR_SUCC)
                goto error;
        }
#if APM_ORA_DBG
        ORA_PRINT_DBG("skip %d bytes after uacBufLength\n", bytebuff_getPosition(p_st_ora_packet->p_st_packet) - i_pos_start);
#endif
    }
    else
    {
        if (ora_mar_unmarshalDALC(p_st_sess, p_st_ora_packet, &p_tmp, &i_tmp_len) == FR_SUCC) // bitVector
        {
            if (ora_ttc8_tti_rxd_readBitVector(p_st_conn, p_tmp, i_tmp_len) != FR_SUCC)
            {
                ora_free((void **) &p_tmp);
                goto error;
            }
            ora_free((void **) &p_tmp);
        }
        else
            goto error;

        if (ora_mar_skipDALC(p_st_sess, p_st_ora_packet) != FR_SUCC)
            goto error;
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}
